package com.zy.utils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;


/**  
  * 创建人：zhushoulong
  * 创建时间：2013-11-12 上午11:23:40 
  * 类描述：  邮件发送器   
  * @version      
*/ 
public class SimpleMailSender {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(SimpleMailSender.class);

	/**
	*用spring mail 发送邮件,依赖jar：spring.jar，activation.jar，mail.jar 
	*/
	public static String sendFileMail(String toMail,String formMail,String password,String code,String title) {
		JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();
		// 设定mail server
		//senderImpl.setHost("smtp.163.com");
		/*126邮箱 
		 POP3服务器:pop.126.com    SMTP服务器:smtp.126.com 
		163邮箱 
		 POP3服务器:pop.163.com    SMTP服务器:smtp.163.com 
		 
		yahoo邮箱 
		POP3服务器：pop.mail.yahoo.com.cn    SMTP服务器：smtp.mail.yahoo.com.cn 
		 
		Sohu邮箱 
		POP3服务器：pop3.sohu.com     SMTP服务器：smtp.sohu.com  
		 
		Gmail邮箱 
		POP3服务器是pop.gmail.com            SMTP服务器是smtp.gmail.com
		  
		QQ邮箱 
		  POP3服务器：pop.qq.com    SMTP服务器：smtp.qq.com */  
		boolean bolg = true;
		if(formMail.indexOf("@163") != -1){
			senderImpl.setHost("smtp.163.com");
		}else if(formMail.indexOf("@139") != -1){
			senderImpl.setHost("smtp.139.com");
		}else if(formMail.indexOf("@yahoo") != -1){
			senderImpl.setHost("smtp.mail.yahoo.com.cn");
		}else if(formMail.indexOf("@sohu") != -1){
			senderImpl.setHost("smtp.sohu.com");
		}else if(formMail.indexOf("@126") != -1){
			senderImpl.setHost("smtp.126.com");
		}else if(formMail.indexOf("@qq") != -1){
			senderImpl.setHost("smtp.qq.com");
		}else{
			bolg = false;
		}
		
		if(bolg){
			try {
				senderImpl.setUsername(formMail);
				senderImpl.setPassword(password);
				// 建立HTML邮件消息
				MimeMessage mailMessage = senderImpl.createMimeMessage();
				// true表示开始附件模式
				MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, false , "utf-8");

				// 设置收件人，寄件人
				messageHelper.setTo(toMail);
				messageHelper.setFrom(formMail);
				messageHelper.setSubject(title);
				// true 表示启动HTML格式的邮件
				messageHelper.setText("<html><head></head><body><h3>"+code+"</h3></body></html>", true);
//			messageHelper.setText("<html><head></head><body><h3>易购用户密码修复</h3>亲爱的易购用户，您好!<br />"+
//			"请您输入下面的六位数验证码来修复易购登录密码.<br />"+code+
//			"<br /><br /><br /><br />为了确保您的帐号安全，该验证码仅10分钟内有效。</body></html>", true);

//			FileSystemResource file1 = new FileSystemResource(new File("e:/logo.jpg"));
//			FileSystemResource file2 = new FileSystemResource(new File("e:/读书.txt"));
				// 添加2个附件
//			messageHelper.addAttachment("logo.jpg", file1);
				
//			try {
				//附件名有中文可能出现乱码
//				messageHelper.addAttachment(MimeUtility.encodeWord("读书.txt"), file2);
//			} catch (UnsupportedEncodingException e) {
//				e.printStackTrace();
//				throw new MessagingException();
//			}
				// 发送邮件
				senderImpl.send(mailMessage);
//			System.out.println("邮件发送成功.....");
			} catch (Exception e) {
				logger.error(e);
//				return "邮件发送失败,请检查是否用户名密码错误!";
				/*String err = "";
				try {
					err = exception(e);
					System.out.println(err);
				} catch (IOException e1) {
					e1.printStackTrace();
				}*/
				String str = "email error";
				return str;
			}
//			return "邮件发送成功!";
			return "eamil success";
		}else{
			return "Activation email failure,Systems support the sender email types are: yahoo,sohu,163,139,126,qq";
		}
	}
	
	//将异常转化为字符串
	private static String exception(Throwable t) throws IOException{
        if(t == null)
            return null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try{
            t.printStackTrace(new PrintStream(baos));
        }finally{
            baos.close();
        }
        return baos.toString();
    }
}
